package com.example.musicgui.tools;

import com.example.musicgui.client.AudioClient;
import com.example.musicgui.client.AudioStatus;
import com.example.musicgui.model.Music;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;
import java.util.List;

public class Audio {
    private AudioClient audioClient;
    public Integer i;

    public Audio() {

    }

    public static Audio getAudio() {
        return new Audio();
    }

    public void status() {
        AudioStatus status = new AudioStatus(audioClient);
        status.start();
    }

    public void close() {
        audioClient.close();
    }

    public void audioMusic(List<Music> music, int i, String audioType) {
        switch (audioType) {
            case "list":
                audioMusicList(music, i);
            default:
                break;
        }
    }

    public void audioMusicList(List<Music> music, int i) {
        int size = music.size();
        while (i < size) {
            this.i = i;
            int j = i; // 使用新的变量保存i的值
            try {
                this.audioClient = new AudioClient();
                System.out.println(i + " = " + size);
                audioClient.audio(music.get(j).getUrl());
            } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
                throw new RuntimeException(e);
            }
            if (i == size-1) {
                i = 0;
            } else {
                i++;
            }
        }
    }
}